package net.whg.service.impl;

import net.whg.mapper.shop.ProductMapper;
import net.whg.mapper.user.AccountMapper;
import net.whg.model.AccountDO;
import net.whg.model.ProductDO;
import net.whg.service.AccountService;
import net.whg.util.CommonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.TransactionTemplate;

import javax.annotation.Resource;
import java.util.List;
import java.util.Random;

@Service
public class AccountServiceImpl implements AccountService {

    @Resource
    private AccountMapper accountMapper;

    @Resource
    private ProductMapper productMapper;

    @Autowired
    private TransactionTemplate userTransactionTemplate;

    @Autowired
    private TransactionTemplate shopTransactionTemplate;

    @Override
    public void initAccount() {
        Random random = new Random();
        AccountDO accountDO = AccountDO.builder().accountNo(Long.valueOf(random.nextInt(100)))
                .phone(CommonUtil.getRandomCode(11)).build();
        accountMapper.insert(accountDO);
    }

    @Override
    public List<AccountDO> queryAccount() {
        return accountMapper.selectList(null);
    }

    @Override
    public List<ProductDO> queryProduct() {
        return productMapper.selectList(null);
    }

    /**
     * 指定主库的事务管理器，这种方式只能针对一个事务管理器进行控制，无法完成多数据源控制
     * 特殊情况：
     * 1.如果主库出现异常，可以
     * 2.如果从库出现异常，可以
     * 3.异常出现在主库和从库中间，可以
     * 除非主库和从库之后进行的业务出现异常，则不能保证事务的一致性，从库的数据不会回滚
     */
    @Override
    @Transactional
    public void saveAll() {
        Random random = new Random();
        //操作user库
        AccountDO accountDO = AccountDO.builder().accountNo(Long.valueOf(random.nextInt(100)))
                .phone(CommonUtil.getRandomCode(11)).build();
        accountMapper.insert(accountDO);
        //操作shop库
        ProductDO productDO = ProductDO.builder().title("产品" + random.nextInt(100)).build();
        productMapper.insert(productDO);
        //发生异常
        int i = 1 / 0;
    }


    @Override
    public void saveAllNew() {
        userTransactionTemplate.execute(userStatus -> {
            shopTransactionTemplate.execute(shopStatus -> {
                try{
                    Random random = new Random();
                    //操作user库
                    AccountDO accountDO = AccountDO.builder().accountNo(Long.valueOf(random.nextInt(100)))
                            .phone(CommonUtil.getRandomCode(11)).build();
                    accountMapper.insert(accountDO);
                    //操作shop库
                    ProductDO productDO = ProductDO.builder().title("产品" + random.nextInt(100)).build();
                    productMapper.insert(productDO);
                    //发生异常
                    int i = 1 / 0;
                    return true;
                }catch (Exception e){
                    e.printStackTrace();
                    userStatus.setRollbackOnly();
                    shopStatus.setRollbackOnly();
                    return false;
                }
            });
            return true;
        });
    }
}
